package org.goldenmock.web.server;

import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.io.Files;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * Builder methods for EasyHttpResponse, grouped together in this seperate file in
 * order to keep complexity to a minimum.
 *
 * @author Robin Wagenaar
 * @author Arjen van Staalduinen
 */
@SuppressWarnings("unused")
public class EasyHttpResponseBuilder extends EasyHttpResponse {

    public EasyHttpResponseBuilder withHttpCode(int httpCode) {
        super.setHttpCode(httpCode);
        return this;
    }

    public EasyHttpResponseBuilder withHeader(String paramName, String paramValue) {
        super.addHeader(paramName, paramValue);
        return this;
    }

    public EasyHttpResponseBuilder withHeaders(Map<String, String> headers){
        super.addHeaders(headers);
        return this;
    }

    public EasyHttpResponseBuilder withReturningHeader(String header){
        super.addReturningHeader(header);
        return this;
    }

    public EasyHttpResponseBuilder withContent(String content) {
        super.setContent(content);
        return this;
    }

    public EasyHttpResponseBuilder withContentType(String contentType) {
        super.setContentType(contentType);
        return this;
    }

    public EasyHttpResponseBuilder fromFile(String filename) {
        try {
            String fileContent = Files.toString(new File(filename), Charsets.UTF_8);
            super.setContent(fileContent);
            return this;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public EasyHttpResponseBuilder withHeaderFromRequest(String headerName) {
        super.addReturningHeader(headerName);
        return this;
    }

    public EasyHttpResponseBuilder withVariable(String name, Object value) {
        super.addVariable(name, value);
        return this;
    }

    public EasyHttpResponseBuilder withVariables(Map<String, Object> vars) {
        super.addVariables(vars);
        return this;
    }

    public EasyHttpResponseBuilder withRenderFunction(Function<EasyHttpResponse, EasyHttpResponse> renderFunction) {
        super.setRenderFunction(renderFunction);
        return this;
    }
}
